package com.liliya.servlet.ad;

import com.liliya.pojo.Ad;
import com.liliya.service.AdService;
import com.liliya.service.impl.AdServiceImpl;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * @Description TODO
 * @Author hhy
 * @Date 2021/1/13 11:54
 */
@WebServlet(urlPatterns = "/ad/manage")
public class AdManageServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        AdService adService = new AdServiceImpl();
        int total = adService.adTotal();
        int num = 1;
        String p = req.getParameter("p");
        if (p!=null){
            num = Integer.parseInt(p);
        }
        Ad ad = adService.findAd(num);
        int pre = num-1<1?num:num-1;
        int next = num+1>total?num:num+1;
        req.setAttribute("ad",ad);
        req.setAttribute("pre",pre);
        req.setAttribute("next",next);
        req.setAttribute("total",total);

        req.getRequestDispatcher("./manage.jsp").forward(req,resp);
    }
}
